package com.gec.anan.driver.service.impl;

import com.gec.anan.common.constant.RedisConstant;
import com.gec.anan.common.execption.AnanException;
import com.gec.anan.common.result.Result;
import com.gec.anan.common.result.ResultCodeEnum;
import com.gec.anan.dispatch.client.NewOrderFeignClient;
import com.gec.anan.driver.client.DriverInfoFeignClient;
import com.gec.anan.driver.service.DriverService;
import com.gec.anan.map.client.LocationFeignClient;
import com.gec.anan.model.form.driver.DriverFaceModelForm;
import com.gec.anan.model.form.driver.UpdateDriverAuthInfoForm;
import com.gec.anan.model.vo.driver.DriverAuthInfoVo;
import com.gec.anan.model.vo.driver.DriverLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {

    @Autowired
    private DriverInfoFeignClient client;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LocationFeignClient locationFeignClient;
    @Autowired
    private NewOrderFeignClient newOrderFeignClient;

    @Override
    public String login(String code) {
        //远程调用
        Result<Long> result = client.login(code);
        //判断driverInfoid是否存在
        if(result.getCode() !=200) {
            throw new AnanException(result.getCode(), result.getMessage());
        }
        Long driverId = result.getData();
        if(driverId == null){
            throw new AnanException(ResultCodeEnum.DATA_ERROR);
        }
        //生成token
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        log.info("token=>{}", token);

        //保存到redis
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token,
                driverId.toString(),RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);

        return token;
    }
    /**
     * 获取司机信息
     */
    @Override
    public DriverLoginVo getDriverInfo(Long driverId) {
        return client.getDriverLoginInfo(driverId).getData();
    }

    /**
     * 获取司机认证信息
     * @param driverId
     * @return
     */
    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
        Result<DriverAuthInfoVo> driverAuthInfo = client.getDriverAuthInfo(driverId);
        if(driverAuthInfo.getCode()==200){
            return driverAuthInfo.getData();
        }else {
            throw new AnanException(driverAuthInfo.getCode(),driverAuthInfo.getMessage());
        }
    }
    /**
     * 司机更新认证信息
     */
    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfo) {
        Result<Boolean> result = client.updateDriverAuthInfo(updateDriverAuthInfo);
        if(!result.getData()){
            throw new AnanException(ResultCodeEnum.DATA_ERROR);
        }
        return true;
    }

    /**
     * 创建司机人脸模型
     * @param driverFaceModelForm
     * @return
     */
    @Override
    public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        Result<Boolean> result = client.creatDriverFaceModel(driverFaceModelForm);
        if(!result.getData()){
            throw new AnanException(ResultCodeEnum.FEIGN_FAIL);
        }
        return result.getData();
    }

    /**
     * 判断司机当日是否进行人脸识别
     * @param driverId
     * @return
     */
    @Override
    public Boolean isFaceRecognition(Long driverId) {
        return client.isFaceRecognition(driverId).getData();
    }

    /**
     * 司机的当日人脸识别
     * @param driverFaceModelForm
     * @return
     */
    @Override
    public Boolean verifyDriverFace(DriverFaceModelForm driverFaceModelForm) {
        Boolean data = client.verifyDriverFace(driverFaceModelForm).getData();
        log.info("人脸识别结果=>{}",data);
        return true;
    }

    /**
     * 开始接单服务
     * @param driverId
     * @return
     */

    @Override
    public Boolean startService(Long driverId) {
        //1是否认证了
        DriverLoginVo driverLoginVo = client.getDriverLoginInfo(driverId).getData();
        if(driverLoginVo.getAuthStatus() !=2){
            throw new AnanException(ResultCodeEnum.AUTH_ERROR);
        }
        //2是否进行过人脸识别
        Boolean isFace = client.isFaceRecognition(driverId).getData();
        if(!isFace){
            throw new AnanException(ResultCodeEnum.FACE_ERROR);
        }
        //3更新接单状态
        client.updateServiceStatus(driverId, 1);
        //4删除redis司机位置信息
       locationFeignClient.removeDriverLocation(driverId);
        //5清空临时队列
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return true;
    }
    /**
     * 停止接单
     */
    @Override
    public Boolean stopService(Long driverId) {
        //更新接单信息
        client.updateServiceStatus(driverId,0);
        //删除redis位置信息
        locationFeignClient.removeDriverLocation(driverId);
        //清空订单队列
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return true;
    }
}
